﻿using Common.Buffer;
using Common.NetWork;
using SuperSocket.Facility.Protocol;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Server
{
    /// <summary>
    /// SuperSocket请求信息
    /// </summary>
    public class MpaleRequestInfo : IRequestInfo
    {
        /// <summary>
        /// 包头
        /// </summary>
        public RecvPacketOpcode PackageHead;//V27 byte

        /// <summary>
        /// 数据
        /// </summary>
        public MaplePacketRead Buffer;

        public string Key
        {
            get { return PackageHead.ToString(); }
        }

        public MpaleRequestInfo(byte[] buff)
        {

            Buffer = new MaplePacketRead(buff);
            PackageHead = ((RecvPacketOpcode)Buffer.ReadByte());
            Buffer = new MaplePacketRead(Buffer.ToArry());
        }
    }

    /// <summary>
    /// 接收过滤器
    /// </summary>
    public class MapleReceiveFilter : FixedHeaderReceiveFilter<MpaleRequestInfo>
    {
        public MapleReceiveFilter()
            : base(4)
        {

        }

        protected override int GetBodyLengthFromHeader(byte[] header, int offset, int length)
        {
            byte[] gg = new byte[length];
            Array.Copy(header, offset, gg, 0, length);
            MaplePacketRead read = new MaplePacketRead(gg);
            int Length = MapleAESOFB.getPacketLength(read.ReadInt());
            //Console.WriteLine("封包大小:" + header.LongLength);
            return Length;
        }

        protected override MpaleRequestInfo ResolveRequestInfo(ArraySegment<byte> header, byte[] bodyBuffer, int offset, int length)
        {
            byte[] Buff = new byte[length];
            Array.Copy(bodyBuffer, offset, Buff, 0, length);
            MpaleRequestInfo info = new MpaleRequestInfo(Buff);
            return info;
        }
    }
}
